﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BEELab.Data;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace BEELab.Logic.ProgramManagement.StageManagement
{
    public static class PresentationElements
    {
        public class PresentationElement
        {
            public PresentationElement(int ID, DataTable data)
            {
                DataRow[] rows = data.Select("ID=" + ID);

                if (rows.Length == 1)
                {
                    DataRow dr = rows[0];
                    this.id = ID;
                    this.name = dr["Name"].ToString();
                    this.description = dr["Description"].ToString();
                    this.content = dr["Content"].ToString();
                }
            }

            public PresentationElement(int ID)
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "SELECT * FROM PresentationElements WHERE ID=@ID";
                command.Parameters.Add(new SqlParameter("@ID", ID));
                DataTable dt = DBHelper.ExecuteQuery(command);

                if (dt.Rows.Count == 1)
                {
                    DataRow dr = dt.Rows[0];
                    this.id = ID;
                    this.name = dr["Name"].ToString();
                    this.description = dr["Description"].ToString();
                    this.content = dr["Content"].ToString();
                }
            }

            private int id;
            public int ID
            {
                get { return id; }
            }

            private string name;
            public string Name
            {
                get { return name; }
            }

            private string description;
            public string Description
            {
                get { return description; }
            }

            private string content;
            public string Content
            {
                get { return content; }
            }
        }

        public static int ImportElement(int ElementID, int StageID, string NamePrefix)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "SELECT * FROM PresentationElements WHERE ID=@ID";
            command.Parameters.Add(new SqlParameter("@ID", ElementID));
            DataTable dt = DBHelper.ExecuteQuery(command);

            if (dt.Rows.Count != 1)
                return -1;

            DataRow dr = dt.Rows[0];

            int ID = CreateElement(StageID, NamePrefix + dr["Name"].ToString(), dr["Description"].ToString(), dr["Content"].ToString());

            return ID;
        }

        public static int CreateElement(int StageID, string Name, string Description, string Content)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "INSERT INTO PresentationElements (StageID, Name, Description, Content) VALUES (@StageID, @Name, @Description, @Content); SELECT SCOPE_IDENTITY();";
            command.Parameters.Add(new SqlParameter("@StageID", StageID));
            command.Parameters.Add(new SqlParameter("@Name", Name));
            command.Parameters.Add(new SqlParameter("@Description", Description));
            command.Parameters.Add(new SqlParameter("@Content", Content));

            return int.Parse(DBHelper.ExecuteScalar(command));
        }

        public static Boolean DeleteElement(int ID)
        {
            ProgramManagement.StageManagement.ContainerManagement.RemoveElement("PresentationElement", ID);

            SqlCommand command = new SqlCommand();
            command.CommandText = "DELETE FROM PresentationElements WHERE ID=@ID";
            command.Parameters.Add(new SqlParameter("@ID", ID));

            int res = DBHelper.ExecuteNonQuery(command);

            if (res > 0)
                return true;

            return false;
        }

        public static int GetIDByName(int StageID, string Name)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "SELECT ID FROM PresentationElements WHERE StageID=@StageID AND Name=@Name";
            command.Parameters.Add(new SqlParameter("@StageID", StageID));
            command.Parameters.Add(new SqlParameter("@Name", Name));
            return int.Parse(DBHelper.ExecuteScalar(command));
        }

        public static Boolean DeleteElement(int StageID, string Name)
        {
            return DeleteElement(GetIDByName(StageID, Name));
        }

        public static Boolean UpdateElement(int ID, string Name, string Description, string Content)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "UPDATE PresentationElements SET Name=@Name, Description=@Description, Content=@Content WHERE ID=@ID";
            command.Parameters.Add(new SqlParameter("@Name", Name));
            command.Parameters.Add(new SqlParameter("@Description", Description));
            command.Parameters.Add(new SqlParameter("@Content", Content));
            command.Parameters.Add(new SqlParameter("@ID", ID));

            int res = DBHelper.ExecuteNonQuery(command);

            if (res == 1)
                return true;

            return false;
        }

        public static List<int> ListByStageID(int ID)
        {
            List<int> result = new List<int>();

            SqlCommand command = new SqlCommand();
            command.CommandText = "SELECT ID FROM PresentationElements WHERE StageID=@StageID";
            command.Parameters.Add(new SqlParameter("@StageID", ID));
            DataTable dt = DBHelper.ExecuteQuery(command);

            foreach (DataRow dr in dt.Rows)
            {
                result.Add((int)dr["ID"]);
            }

            return result;
        }

        public static string GetNameByID(int ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "SELECT Name FROM PresentationElements WHERE ID=@ID";
            command.Parameters.Add(new SqlParameter("@ID", ID));
            return DBHelper.ExecuteScalar(command);
        }

        /// <summary>
        /// Returns presentation layer element content as a string
        /// </summary>
        /// <param name="ID">presentation layer ID</param>
        /// <returns>presentation layer element content</returns>
        public static string GetContent(int ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "SELECT Content FROM PresentationElements WHERE ID=@ID";
            command.Parameters.Add(new SqlParameter("@ID", ID));
            return DBHelper.ExecuteScalar(command);
        }

        /// <summary>
        /// Updates existing presentation layer element
        /// </summary>
        /// <param name="ID">Presentation Elements ID</param>
        /// <param name="Content">New Content</param>
        public static void SetContent(int ID, string Content)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "UPDATE PresentationElements SET Content=@Content WHERE ID=@ID";
            command.Parameters.Add(new SqlParameter("@Content", Content));
            command.Parameters.Add(new SqlParameter("@ID", ID));
            DBHelper.ExecuteNonQuery(command);
        }

        public static void SetName(int ID, string Name)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "UPDATE PresentationElements SET Name=@Name WHERE ID=@ID";
            command.Parameters.Add(new SqlParameter("@Name", Name));
            command.Parameters.Add(new SqlParameter("@ID", ID));
            DBHelper.ExecuteNonQuery(command);
        }

        public static int GetProgramID(int ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "SELECT Stages.ProgramID FROM Stages JOIN PresentationElements ON PresentationElements.StageID=Stages.ID WHERE PresentationElements.ID=@ID";
            command.Parameters.Add(new SqlParameter("@ID", ID));
            return int.Parse(DBHelper.ExecuteScalar(command));
        }

        public static int GetOwnerID(int ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "SELECT Programs.OwnerID FROM PresentationElements JOIN Stages ON PresentationElements.StageID=Stages.ID JOIN Programs ON Programs.ID=Stages.ProgramID WHERE PresentationElements.ID=@ID";
            command.Parameters.Add(new SqlParameter("@ID", ID));
            string res = DBHelper.ExecuteScalar(command);

            if (res != null && res.Length > 0)
            {
                return int.Parse(res);
            }

            return 0;
        }


        public static string EnumerateElementsNew(int StageID, string separator)
        {
            StringBuilder res = new StringBuilder();

            int ProgramID = StageManagement.GetProgramID(StageID);

            SqlCommand command = new SqlCommand();
            command.CommandText = "SELECT PresentationElements.Name FROM PresentationElements JOIN Stages ON PresentationElements.StageID=Stages.ID WHERE Stages.ProgramID=@ProgramID";
            command.Parameters.Add(new SqlParameter("@ProgramID", ProgramID));

            DataTable dt = DBHelper.ExecuteQuery(command);

            bool isFirst = true;

            foreach (DataRow row in dt.Rows)
            {
                if (!isFirst)
                {
                    res.Append(separator);
                }
                else
                {
                    isFirst = false;
                }
                res.Append(row[0].ToString());
                res.Append("$");
            }

            return res.ToString();
        }

        public static string EnumerateElementsEdit(int ID, string separator)
        {
            StringBuilder res = new StringBuilder();

            int ProgramID = GetProgramID(ID);

            SqlCommand command = new SqlCommand();
            command.CommandText = "SELECT PresentationElements.Name FROM PresentationElements JOIN Stages ON PresentationElements.StageID=Stages.ID WHERE Stages.ProgramID=@ProgramID AND PresentationElements.ID!=@ID";
            command.Parameters.Add(new SqlParameter("@ProgramID", ProgramID));
            command.Parameters.Add(new SqlParameter("@ID", ID));

            DataTable dt = DBHelper.ExecuteQuery(command);

            bool isFirst = true;

            foreach (DataRow row in dt.Rows)
            {
                if (!isFirst)
                {
                    res.Append(separator);
                }
                else
                {
                    isFirst = false;
                }
                res.Append(row[0].ToString());
                res.Append("$");
            }

            return res.ToString();
        }
    }
}